package com.xique.api.service.biz.service.impl;

import com.xique.api.bean.request.community.*;
import com.xique.api.bean.vo.community.CommunityInfoVO;
import com.xique.api.constants.OpenPlatformConstants;
import com.xique.api.service.biz.service.ICommunityBizService;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.api.RemoteBuildingService;
import com.xique.door.api.request.build.RemoteAreaListRequest;
import com.xique.door.api.request.build.RemoteBuildingListRequest;
import com.xique.door.api.request.build.RemoteHouseListRequest;
import com.xique.door.api.request.build.RemoteHouseUserListRequest;
import com.xique.system.api.RemoteSystemProjectService;
import com.xique.system.api.dto.SystemProjectDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shd
 * @create 2022/3/5 15:34
 */
@Service
public class CommunityBizServiceImpl implements ICommunityBizService {

    @Autowired
    private RemoteSystemProjectService projectService;

    @Autowired
    private RemoteBuildingService buildingService;

    private AjaxResult validationService() {
        String type = SecurityUtils.getForeignType();
        if (OpenPlatformConstants.TokenType.SERVICE.equals(type)) {
            return AjaxResult.error("抱歉，服务商账号该功能未开启");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getCommunityInfo(CommunityInfoRequest request) {

        AjaxResult ajaxResult = validationService();
        if (AjaxResult.isError(ajaxResult)) {
            return ajaxResult;
        }
        Long projectId = SecurityUtils.getForeignId();
        R<SystemProjectDTO> result = projectService.getProjectInfo(projectId);
        if (R.FAIL == result.getCode()) {
            return AjaxResult.error(result.getMsg());
        }
        CommunityInfoVO vo = OrikaUtil.convert(result.getData(), CommunityInfoVO.class);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getAreaList(CommunityAreaListRequest request) {

        AjaxResult ajaxResult = validationService();
        if (AjaxResult.isError(ajaxResult)) {
            return ajaxResult;
        }
        Long projectId = SecurityUtils.getForeignId();
        RemoteAreaListRequest listRequest = OrikaUtil.convert(request, RemoteAreaListRequest.class);
        listRequest.setProjectId(projectId);

        return buildingService.getAreaList(listRequest);
    }

    @Override
    public AjaxResult getBuildingList(CommunityBuildingListRequest request) {

        AjaxResult ajaxResult = validationService();
        if (AjaxResult.isError(ajaxResult)) {
            return ajaxResult;
        }
        Long projectId = SecurityUtils.getForeignId();
        RemoteBuildingListRequest listRequest = OrikaUtil.convert(request, RemoteBuildingListRequest.class);
        listRequest.setProjectId(projectId);
        return buildingService.getBuildingList(listRequest);
    }

    @Override
    public AjaxResult getHouseList(CommunityHouseListRequest request) {

        AjaxResult ajaxResult = validationService();
        if (AjaxResult.isError(ajaxResult)) {
            return ajaxResult;
        }
        Long projectId = SecurityUtils.getForeignId();
        RemoteHouseListRequest listRequest = OrikaUtil.convert(request, RemoteHouseListRequest.class);
        listRequest.setProjectId(projectId);

        return buildingService.getHouseList(listRequest);
    }

    @Override
    public AjaxResult getHouseUserList(CommunityHouseUserListRequest request) {

        AjaxResult ajaxResult = validationService();
        if (AjaxResult.isError(ajaxResult)) {
            return ajaxResult;
        }
        Long projectId = SecurityUtils.getForeignId();
        RemoteHouseUserListRequest listRequest = OrikaUtil.convert(request, RemoteHouseUserListRequest.class);
        listRequest.setProjectId(projectId);

        return buildingService.getHouseUserList(listRequest);
    }
}
